package com.scd.boot.mvc.exception;

import com.scd.boot.enums.RequestCodeEnum;
import com.scd.boot.exception.CheckSignException;
import com.scd.boot.exception.ParamException;
import com.scd.boot.model.BaseResponse;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(ParamException.class)
    public BaseResponse<String> handlerParamException(HttpServletRequest request, ParamException e) {
        log.info("request {} error ", request.getRequestURI(), e);
        return new BaseResponse<>(RequestCodeEnum.PARAM_ERROR.getCode(), e.getMessage());
    }

    @ExceptionHandler(CheckSignException.class)
    public BaseResponse<String> handlerCheckSignException(HttpServletRequest request, CheckSignException e) {
        log.info("request {} error ", request.getRequestURI(), e);
        return new BaseResponse<>(RequestCodeEnum.SIGN_ERROR.getCode(), e.getMessage());
    }
}
